{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character in identifier (Temp/ipykernel_5992/3601166892.py, line 11)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"C:\\Users\\sjc\\AppData\\Local\\Temp/ipykernel_5992/3601166892.py\"\u001b[1;36m, line \u001b[1;32m11\u001b[0m\n\u001b[1;33m    os.environ[‘CUDA_VISIBLE_DEVICES’] = ‘0’\u001b[0m\n\u001b[1;37m                                    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character in identifier\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import tensorflow.keras\n",
    "from tensorflow.keras import layers\n",
    "\n",
    "import os\n",
    "from sklearn import preprocessing #用于标准化\n",
    "#开启gpu\n",
    "os.environ[‘CUDA_VISIBLE_DEVICES’] = ‘0’\n",
    "\n",
    "features=pd.read_csv(‘housing.csv’)\n",
    "print(features.head())\n",
    "print(features.shape)\n",
    "\n",
    "#标签\n",
    "labels = np.array(features[‘MEDV’])\n",
    "features=features.drop(‘MEDV’, axis=1)\n",
    "#名字单独保存一下，以备后患\n",
    "\n",
    "features =np.array(features)\n",
    "\n",
    "#数据进行标准化\n",
    "input_features=preprocessing.StandardScaler().fit_transform(features)\n",
    "print(input_features[0])\n",
    "\n",
    "#新建一个model，一个神经网络\n",
    "model=tf.keras.Sequential()\n",
    "model.add(layers.Dense(16))\n",
    "model.add(layers.Dense(32))\n",
    "model.add(layers.Dense(1))\n",
    "\n",
    "#设定模型的学习率和损失函数\n",
    "model.compile(optimizer=tf.keras.optimizers.SGD(0.001),loss=‘mean_squared_error’)\n",
    "#设定模型的输入值，x和y，训练的样本，轮数，\n",
    "model.fit(input_features,labels,validation_split=0.25,epochs=10,batch_size=64)\n",
    "#model.summary() #查看模型的构成\n",
    "\n",
    "predict = model.predict(input_features)\n",
    "\n",
    "plt.figure(figsize=(20,10)) #设置窗口的大小\n",
    "plt.ylabel(‘Price’)\n",
    "plt.plot(labels,‘b-’,label=‘true’)\n",
    "plt.plot(predict,‘ro’,label=‘predict’)\n",
    "plt.legend()\n",
    "plt.show()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
